package com.android.lovingpet.community.post;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.lovingpet.R;
import com.bumptech.glide.Glide;

import java.io.File;
import java.util.List;

public class AlbumAdapter extends RecyclerView.Adapter<AlbumAdapter.ViewHolder> {

    private Context context;
    private List<String> images;
    private Callback listener;
    private boolean isShowDelete;

    public AlbumAdapter(List<String> images) {
        this.images = images;
    }

    public void setImages(List<String> images) {
        this.images = images;
    }

    public void setListener(Callback listener) {
        this.listener = listener;
    }

    public void setShowDelete(boolean showDelete) {
        isShowDelete = showDelete;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view = LayoutInflater.from(context).inflate(R.layout.item_image, parent, false);
        ViewHolder holder = new ViewHolder(view);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (images.get(holder.getAdapterPosition()) == null && listener != null) {
                    listener.onPick(v);
                }
            }
        });
        view.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (listener != null) {
                    listener.toDelete();
                }
                return true;
            }
        });
        view.findViewById(R.id.iv_delete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.notifyDataSetChanged(holder.getAdapterPosition());
                }
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        String image = images.get(position);
        if (image != null) {
            Glide.with(context).load(image).into(holder.ivImage);
            if (isShowDelete) {
                holder.ivDelete.setVisibility(View.VISIBLE);
            }
        } else {
            holder.ivDelete.setVisibility(View.GONE);
            Glide.with(context).load(R.drawable.ic_add_border).into(holder.ivImage);
        }
    }

    @Override
    public int getItemCount() {
        return images.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView ivImage;
        ImageView ivDelete;

        public ViewHolder(View itemView) {
            super(itemView);
            ivImage = itemView.findViewById(R.id.iv_image);
            ivDelete = itemView.findViewById(R.id.iv_delete);
        }
    }

    public interface Callback {
        void onPick(View v);
        void toDelete();
        void notifyDataSetChanged(int position);
    }
}
